<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="csrf-token" content="{:token()}">
    <link rel="stylesheet" href="/static/backend/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/backend/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/static/backend/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">菜单名称</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="required|title" lay-reqtext="菜单名称不能为空" placeholder="请输入菜单名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">上级菜单</label>
        <div class="layui-input-block">
            <select name="pid" lay-verify="required" lay-search>
                <option value="0"></option>
                {foreach $menuList as $v}
                <option value="{$v.id}">{$v.title}</option>
                {/foreach}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单图标</label>
        <div class="layui-input-block">
            <input type="text" id="iconPicker" lay-filter="iconPicker" class="hide" name="icon" value="">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">链接</label>
        <div class="layui-input-block">
            <input type="text" name="href" lay-verify="href" lay-reqtext="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">链接打开</label>
        <div class="layui-input-block">
            <input type="text" name="target" lay-verify="target" lay-reqtext="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="text" name="sort" lay-verify="number|sort" lay-reqtext="" value="0" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">controller</label>
        <div class="layui-input-block">
            <input type="text" name="controller" lay-verify="controller" lay-reqtext="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">action</label>
        <div class="layui-input-block">
            <input type="text" name="action" lay-verify="action" lay-reqtext="" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态与导航菜单</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" title="禁用">
            <input type="checkbox" name="is_nav" title="导航菜单">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="/static/backend/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/backend/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'layer', 'iconPickerFa'], function () {
        var form = layui.form,
            layer = layui.layer,
            iconPickerFa = layui.iconPickerFa,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.post('/backend/menu/save', data.field, function (res) {
                if (res.code == 0) {
                    // 添加成功
                    layer.msg(res.msg, {
                        icon:1,
                        time: 1000
                    },function () {
                        // 重新加载父页面
                        parent.location.reload();
                    });
                } else if (res.code == 302) {
                    location.href = '/backend/error/' + res.msg;
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'json');
            return false;
        });

        // 图标选择器
        iconPickerFa.render({
            // 选择器，推荐使用input
            elem: '#iconPicker',
            // fa 图标接口
            url: "/static/backend/lib/font-awesome-4.7.0/less/variables.less",
            // 是否开启搜索：true/false，默认true
            search: true,
            // 是否开启分页：true/false，默认true
            page: true,
            // 每页显示数量，默认12
            limit: 12,
            // 点击回调
            click: function (data) {
            },
            // 渲染成功后的回调
            success: function (d) {
            }
        });

        // 自定义验证规则
        form.verify({
            title: function (value, item) {
                if (value.length > 50) {
                    return '名称不能超过50个字符';
                }
            },
            href: function (value, item) {
                if (value.length > 100) {
                    return '链接不能超过100个字符';
                }
            },
            target: function (value, item) {
                if (value.length > 20) {
                    return '链接打开方式不能超过20个字符';
                }
            },
            sort: function (value, item) {
                if (value.length > 10) {
                    return '菜单排序不能超过10个字符';
                }
            },
            controller: function (value, item) {
                if (value.length > 30) {
                    return 'controller不能超过30个字符';
                }
            },
            action: function (value, item) {
                if (value.length > 30) {
                    return 'action不能超过30个字符';
                }
            }
        });
    });
</script>
</body>
</html>